package com.cgbl.cg.vm.course

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableField
import api.CourseServiceFactory
import api.ResourceServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.CommentBean
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.model.CourseInfoBean
import com.robot.base.model.PageListBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class CommentProfileViewModel(application: Application) : GLBaseViewModel(application) {

    private var productId = ""
    private var commentType = 0
    private var parentId = "0"
    private var accountId = ""
    var totalField = ObservableField<String>()

    var page = 1

    var pageSize = 10

    var total = ""

    val uc = UiChangeEvent()

    val toInputCommentCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.Mine.A_MINE_COMMENT_INPUT,
            Bundle().apply {
                putInt(
                    AppConstants.BundleKey.COMMENT_PRODUCT_TYPE,
                    commentType
                )
                putString(AppConstants.BundleKey.COMMENT_PRODUCT_ID, productId)
                putString(AppConstants.BundleKey.COMMENT_PARENT_ID, parentId)
                putString(AppConstants.BundleKey.COMMENT_ACCOUNT_ID, accountId)
            })
    })


    fun refresh() {
        page = 1
        fetchCourseComment()
    }

    fun loadMore() {
        page++
        fetchCourseComment()
    }

    fun fetchCourseComment() {
        when(commentType) {
            AppConstants.BundleValue.COMMENT_TYPE_RESOURCE -> {
                val params = mapOf(
                    "allOff" to "1",
                    "type" to "0",
                    "naturalId" to productId,
                    "pageNo" to page,
                    "pageSize" to pageSize
                )
                ResourceServiceFactory.pageComment(params).subscribe(object :
                    AbstractViewModelSubscriber<BaseResponse<PageListBean<CommentBean>>>(this) {
                    override fun onNext(t: BaseResponse<PageListBean<CommentBean>>) {
                        totalField.set("全部评价（${t.data.total}条）")
                        if (page == 1) {
                            uc.refreshCommentEvent.postValue(t.data.list)
                        } else {
                            uc.loadMoreCommentEvent.postValue(t.data.list)
                        }
                    }
                })
            }
            else->{
                val params = mapOf(
                    "allOff" to "1",
                    "type" to "0",
                    "courseId" to productId,
                    "pageNo" to page,
                    "pageSize" to pageSize
                )
                CourseServiceFactory.pageComment(params).subscribe(object :
                    AbstractViewModelSubscriber<BaseResponse<PageListBean<CommentBean>>>(this) {
                    override fun onNext(t: BaseResponse<PageListBean<CommentBean>>) {
                        totalField.set("全部评价（${t.data.total}条）")
                        if (page == 1) {
                            uc.refreshCommentEvent.postValue(t.data.list)
                        } else {
                            uc.loadMoreCommentEvent.postValue(t.data.list)
                        }
                    }
                })
            }
        }
    }

    fun likeComment(id: String, success: (uploadBean: String) -> Unit){
        when(commentType){
            AppConstants.BundleValue.COMMENT_TYPE_RESOURCE->{
                ResourceServiceFactory.createCommentLike(
                    mapOf(
                        "commentId" to id
                    )
                ).subscribe(object :AbstractViewModelSubscriber<BaseResponse<String>>(this){
                    override fun onNext(t: BaseResponse<String>) {
                        success.invoke(t.data)
                    }
                })
            }
            else->{
                CourseServiceFactory.createCommentLike(
                    mapOf(
                        "commentId" to id
                    )
                ).subscribe(object :AbstractViewModelSubscriber<BaseResponse<String>>(this){
                    override fun onNext(t: BaseResponse<String>) {
                        success.invoke(t.data)
                    }
                })
            }
        }
    }

    fun initParams(type: Int, productId: String, parentId: String, accountId: String) {
        this.commentType = type
        this.productId = productId
        this.parentId = parentId
        this.accountId = accountId
    }

    class UiChangeEvent {
        val selectType: SingleLiveEvent<ArrayList<CourseAllTypeBean>> = SingleLiveEvent()

        val refreshCommentEvent: SingleLiveEvent<List<CommentBean>> = SingleLiveEvent()

        val loadMoreCommentEvent: SingleLiveEvent<List<CommentBean>> = SingleLiveEvent()

    }
}